package app.auto.runner.base.intf;

import android.app.Activity;
import android.nfc.NfcEvent;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;


import app.auto.runner.base.ActivityStack;
import app.auto.runner.base.utility.ShareAlert;
import app.auto.runner.base.utility.ZhUtil;
import app.auto.runner.base.utility.base.Toastial;


/**
 * Created by litli on 2017/6/23.
 */

public class ValueCallback<S, F, C> extends FunCallback {

    String url;
    Map map;

    public void setUrl(String url) {
        this.url = url;
    }

    public void setParamsMap(Map map) {
        this.map = map;
    }

    public Map m;
    private FunCallback fab;
    private String jsonparsefield;
    private boolean cat;

    public ValueCallback setFab(FunCallback fab) {
        this.fab = fab;
        return this;
    }

    public ValueCallback setM(Map m) {
        this.m = m;
        return this;
    }

    public ValueCallback setIfJsonParseField(String jsonparsefield) {
        this.jsonparsefield = jsonparsefield;
        return this;
    }

    public boolean isdefault = true;

    public ValueCallback setIsdefault(boolean isdefault) {
        this.isdefault = isdefault;
        return this;
    }


    public String getUrl() {
        return url;
    }

    public Map getMap() {
        return map;
    }

    @Override
    public void onCallback(Object result, Object object) {
        if (result.toString().contains("error:")) {
            ShareAlert.shareAlert(ActivityStack.publicactivity, url, map.toString(), result.toString(), "");
            return;
        }
        if (m != null && m.size() > 0) {
            boolean met = false;
            for (Object o : m.keySet()) {
                if (result.toString().contains(o.toString())) {
                    Object value = m.get(o);
                    value = value.toString().replaceAll("\\r", "").replaceAll("\\n", "");
                    met = true;
                    Toastial.quick(value.toString());
                    if (fab != null) {
                        fab.onCallback(value.toString(), object);
                    }
                }
            }
            if (!met) {
                Toastial.quick(result.toString());
            }
        } else if (jsonparsefield != null && !jsonparsefield.equals("")) {

                String msg = JsonUtil.findJsonLink(jsonparsefield,result.toString()).toString();
//                Toastial.quick(msg);

        } else if (cat) {
            String ch = ZhUtil.catZh(result);
            if (!ch.equals("")) {
                Toastial.quick(ch.toString());
                fab.onCallback(result, ch);
                return;
            }
        } else if (isdefault) {
            Toastial.quick(result.toString());
        }

        if (fab != null) {
            fab.onCallback(result, object);
        }
    }


    public ValueCallback setCat(boolean cat) {
        this.cat = cat;
        return this;
    }


    public void simpleRun(Object aty) {

    }

    ;
}
